*****************
* basic template
*****************

use "scad_nelda_basic.dta", clear
desc
count

**************
* Merge NELDA
**************
merge 1:1 ccode day using "nelda_4merge.dta", keepusing(count_*)
drop if _merge == 2
drop _merge


*************
* Merge SCAD
*************

merge 1:1 ccode day using "scad_4merge.dta", keepusing(numb_*)
drop if _merge == 2
drop _merge

************************
* rename and label vars
************************
replace count_ass = 0 if count_ass == .
replace count_exec = 0 if count_exec == .
replace count_leg = 0 if count_leg == .
egen count_all = rowtotal(count_ass count_exec count_leg)

replace count_ass_contest = 0 if count_ass_contest == .
replace count_exec_contest = 0 if count_exec_contest == .
replace count_leg_contest = 0 if count_leg_contest == .
egen count_all_contest = rowtotal(count_ass_contest count_exec_contest count_leg_contest)

replace count_ass_NOcontest = 0 if count_ass_NOcontest == .
replace count_exec_NOcontest = 0 if count_exec_NOcontest == .
replace count_leg_NOcontest = 0 if count_leg_NOcontest == .
egen count_all_NOcontest = rowtotal(count_ass_NOcontest count_exec_NOcontest count_leg_NOcontest)

replace numb_allevents = 0 if numb_allevents == .
replace numb_progovt = 0 if numb_progovt == .
replace numb_antigovt = 0 if numb_antigovt == .

rename count_ass numbelec_ass
rename count_exec numbelec_exec
rename count_leg numbelec_leg
rename count_all numbelec_all

rename count_ass_contest numbelec_ass_contest
rename count_exec_contest numbelec_exec_contest
rename count_leg_contest numbelec_leg_contest
rename count_all_contest numbelec_all_contest

rename count_ass_NOcontest numbelec_ass_NOcontest
rename count_exec_NOcontest numbelec_exec_NOcontest
rename count_leg_NOcontest numbelec_leg_NOcontest
rename count_all_NOcontest numbelec_all_NOcontest

label variable numbelec_ass "Number of Elections (Assembly)"
label variable numbelec_exec "Number of Elections (Executive)"
label variable numbelec_leg "Number of Elections (Legistlative)"
label variable numbelec_all "Number of Elections (All)"

label variable numbelec_ass_contest "Number of Elections (Assembly with Contest)"
label variable numbelec_exec_contest "Number of Elections (Executive with Contest)"
label variable numbelec_leg_contest "Number of Elections (Legistlative with Contest)"
label variable numbelec_all_contest "Number of Elections (All with Contest)"

label variable numbelec_ass_NOcontest "Number of Elections (Assembly with NO Contest)"
label variable numbelec_exec_NOcontest "Number of Elections (Executive with NO Contest)"
label variable numbelec_leg_NOcontest "Number of Elections (Legistlative with NO Contest)"
label variable numbelec_all_NOcontest "Number of Elections (All with NO Contest)"


************
* Save file
************

save "scad_nelda_master.dta", replace




*********************
* YEAR-LEVEL DATASET
*********************


*****************
* basic template
*****************

use "scad_nelda_basic_year.dta", clear
desc
count


**************
* Merge NELDA
**************
merge 1:1 ccode year using "nelda_year_4merge.dta", keepusing(count_*)
drop if _merge == 2
drop _merge


*************
* Merge SCAD
*************
merge 1:1 ccode year using "scad_year_4merge.dta", keepusing(countryname numb_allevents)
drop if _merge == 2
drop _merge



************************
* Rename and label vars
************************

replace count_ass = 0 if count_ass == .
replace count_exec = 0 if count_exec == .
replace count_leg = 0 if count_leg == .
egen count_all = rowtotal(count_ass count_exec count_leg)

replace count_ass_contest = 0 if count_ass_contest == .
replace count_exec_contest = 0 if count_exec_contest == .
replace count_leg_contest = 0 if count_leg_contest == .
egen count_all_contest = rowtotal(count_ass_contest count_exec_contest count_leg_contest)

replace numb_allevents = 0 if numb_allevents == .

rename count_ass numbelec_ass
rename count_exec numbelec_exec
rename count_leg numbelec_leg
rename count_all numbelec_all

rename count_ass_contest numbelec_ass_contest
rename count_exec_contest numbelec_exec_contest
rename count_leg_contest numbelec_leg_contest
rename count_all_contest numbelec_all_contest

label variable numbelec_ass "Number of Elections (Assembly)"
label variable numbelec_exec "Number of Elections (Executive)"
label variable numbelec_leg "Number of Elections (Legistlative)"
label variable numbelec_all "Number of Elections (All)"

label variable numbelec_ass_contest "Number of Elections (Assembly with Contest)"
label variable numbelec_exec_contest "Number of Elections (Executive with Contest)"
label variable numbelec_leg_contest "Number of Elections (Legistlative with Contest)"
label variable numbelec_all_contest "Number of Elections (All with Contest)"


************
* Save file
************

save "scad_nelda_year_master.dta", replace




*****************************
* YEAR MONTH -LEVEL DATASET
*****************************


*****************
* basic template
*****************

use "scad_nelda_basic_yearmonth.dta", clear
desc
count


***************
* Merge NELDA
***************
merge 1:1 ccode year month using "nelda_month_4merge.dta", keepusing(count_*)
drop if _merge == 2
drop _merge

*************
* Merge SCAD
*************
merge 1:1 ccode year month using "scad_month_4merge.dta", keepusing(countryname numb_allevents)
drop if _merge == 2
drop _merge

************************
* Rename and label vars
************************

replace count_ass = 0 if count_ass == .
replace count_exec = 0 if count_exec == .
replace count_leg = 0 if count_leg == .
egen count_all = rowtotal(count_ass count_exec count_leg)

replace count_ass_contest = 0 if count_ass_contest == .
replace count_exec_contest = 0 if count_exec_contest == .
replace count_leg_contest = 0 if count_leg_contest == .
egen count_all_contest = rowtotal(count_ass_contest count_exec_contest count_leg_contest)

replace numb_allevents = 0 if numb_allevents == .

rename count_ass numbelec_ass
rename count_exec numbelec_exec
rename count_leg numbelec_leg
rename count_all numbelec_all

rename count_ass_contest numbelec_ass_contest
rename count_exec_contest numbelec_exec_contest
rename count_leg_contest numbelec_leg_contest
rename count_all_contest numbelec_all_contest

label variable numbelec_ass "Number of Elections (Assembly)"
label variable numbelec_exec "Number of Elections (Executive)"
label variable numbelec_leg "Number of Elections (Legistlative)"
label variable numbelec_all "Number of Elections (All)"


label variable numbelec_ass_contest "Number of Elections (Assembly with Contest)"
label variable numbelec_exec_contest "Number of Elections (Executive with Contest)"
label variable numbelec_leg_contest "Number of Elections (Legistlative with Contest)"
label variable numbelec_all_contest "Number of Elections (All with Contest)"


************
* Save file
************
save "scad_nelda_year_month_master.dta", replace

